package org.zerg.controller.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zerg.dto.OrderSta;
import org.zerg.entity.Order;
import org.zerg.entity.OrderProducts;
import org.zerg.service.OrderService;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/api")
public class OrderController {
	
	@Autowired
	private OrderService orderService;

	@RequestMapping(value="/order", method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public OrderSta getOrder(HttpServletRequest request){

		String token = request.getHeader("token");
		String param = request.getParameter("products");
		boolean pass = true;
		List<OrderProducts> params = JSON.parseArray(param, OrderProducts.class);
		String orderNo = orderService.insertOrder(token, params);
		OrderSta orderSta = new OrderSta(pass,orderNo);
		return orderSta;
	}
	
	@RequestMapping(value="/order/{no}", method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public Order getOrderDetail(@PathVariable(value="no") String no){
		Order order = orderService.getOrder(no);
		return order;
	}
	
	@RequestMapping(value="/orderid/{id}", method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public Order getOrderDetailById(@PathVariable(value="id") int id){
		Order order = orderService.getOrderById(id);
		return order;
	}
	
	@RequestMapping(value="/order/by_user", method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public List<Order> getOrders(HttpServletRequest request){
		String token = request.getHeader("token");
		String page = request.getParameter("page");
		System.out.println("token:"+token+"page:"+page);
		List<Order> order = orderService.getOrderPage(page,token);
		return order;
	}
	
}
